namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class InternationalAddressCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<InternationalAddressCollection>()
                .DerivesFrom<Collection<InternationalAddress>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("addresses")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            InternationalAddressCollection actual = (
                "<addresses>" +
                    "<address key='work' />" +
                    "<address key='fax' />" +
                "</addresses>").Deserialize<InternationalAddressCollection>();

            Assert.NotNull(actual);

            Assert.NotEmpty(actual);
        }

        [Fact]
        public void deserializeEmpty()
        {
            InternationalAddressCollection actual = "<addresses />".Deserialize<InternationalAddressCollection>();

            Assert.NotNull(actual);

            Assert.Empty(actual);
        }

        [Fact]
        public void serialize()
        {
            InternationalAddressCollection obj = new InternationalAddressCollection
            {
                new InternationalAddress()
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/addresses/address)"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new InternationalAddressCollection());
        }

        [Fact]
        public void op_indexer_string()
        {
            InternationalAddressCollection obj = new InternationalAddressCollection
            {
                new InternationalAddress
                {
                    Code = "ABC123",
                    Key = "work"
                },
                new InternationalAddress
                {
                    Code = "XYZ456",
                    Key = "fax"
                }
            };

            Assert.Same(obj[1], obj["fax"]);
        }

        [Fact]
        public void op_Contains_string()
        {
            InternationalAddressCollection obj = new InternationalAddressCollection
            {
                new InternationalAddress
                {
                    Code = "ABC123",
                    Key = "work"
                }
            };

            Assert.True(obj.Contains("work"));
            Assert.False(obj.Contains("fax"));
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new InternationalAddressCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new InternationalAddressCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new InternationalAddressCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}